﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Assets.AnimationPlayer.Scripts.Util
{
	public class YioksTool: MonoBehaviour
    {
        #region 事件绑定
        /// <summary>
        ///事件绑定
        /// </summary>
        public static void Eve_Bind(GameObject game, EventTriggerType eveType, UnityAction<BaseEventData> eveData)
        {
            EventTrigger trigger = game.GetComponent<EventTrigger>();
            if (trigger == null)
                game.AddComponent<EventTrigger>();
            trigger = game.GetComponent<EventTrigger>();
            if (trigger.triggers == null)
                trigger.triggers = new List<EventTrigger.Entry>();
            // 定义需要绑定的事件类型。并设置回调函数
            EventTrigger.Entry entry = new EventTrigger.Entry();
            // 设置 事件类型
            entry.eventID = eveType;
            // 设置回调函数
            //entry.callback = new EventTrigger.TriggerEvent();
            UnityAction<BaseEventData> callback = eveData;
            entry.callback.AddListener(callback);
            // 添加事件触发记录到GameObject的事件触发组件
            trigger.triggers.Add(entry);
        }
        #endregion


        private static string Get_DirPath()
        {
            string path;
            if (Application.platform == RuntimePlatform.IPhonePlayer)
            {
                path = Application.persistentDataPath + "/";
                path = "/private" + path;
                path = path.Replace("/Documents/", "/Library/Caches/");
            }
            else if (Application.platform == RuntimePlatform.Android)
                path = Application.persistentDataPath + "/";
            else if (Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXEditor)
            {
                path = Application.dataPath + "/";
                string[] paths = path.Split('/');
                path = "/" + paths[1] + "/" + paths[2] + "/Documents/NewAr/";
            }
            else
                path = Application.dataPath + "/StreamingAssets/";
            
            return path;
        }

        public static string Get_DirPath(string dirName)
        {
            string path = Get_DirPath();
            path = path + dirName;

            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            return path;
        }

        #region 文件管理
        //将内容保存成为一个文件
        /// <summary>
        /// 将内容保存成为一个文件
        /// </summary>
        /// <param name="filePath">文件路经</param>
        /// <param name="content">文件内容</param>
        /// <returns>反回成功或失败</returns>
        public static bool File_Save(string filePath, string content)
        {
            string path = filePath;
            try
            {
                using (StreamWriter sw = File.CreateText(path))
                {
                    sw.Write(content);
                    sw.Flush();
                    sw.Close();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        //读取文件内容
        /// <summary>
        /// 读取文件内容
        /// </summary>
        /// <param name="filePath">文件位置</param>
        /// <returns></returns>
        public static string File_Read(string filePath)
        {
            if (File.Exists(@filePath))
            {
                try
                {
                    StreamReader sr = File.OpenText(filePath);
                    StringBuilder output = new StringBuilder();
                    string rl;
                    while ((rl = sr.ReadLine()) != null)
                    {
                        output.Append(rl + "\r\n");
                    }
                    var confignnr = output.ToString();
                    sr.Close();
                    return confignnr;
                }
                catch
                {
                    return "";
                }
            }
            else
            {
                return "";
            }
        }
        #endregion



        public static void File_Log(System.Exception ex)
        {
            File_Log(ex.ToString());
        }
        public static void File_Log(string log)
        {
            //Debug.Log(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ssssss") + "==>" + log);
            string path = Get_DirPath("Log");
            path = path + "/" + System.DateTime.Now.ToString("yyyy_MM_dd") + ".log";
            try
            {
                if (File.Exists(path))
                {
                    StreamWriter sw = File.AppendText(path);
                    sw.Write("\r\n" + log);
                    sw.Flush();
                    sw.Close();
                }
                else
                {
                    try
                    {
                        using (StreamWriter sw = File.CreateText(path))
                        {
                            sw.Write(log);
                            sw.Flush();
                            sw.Close();
                        }
                    }
                    catch
                    {
                        //
                    }
                }
            }
            catch
            {
                //
            }
        }

        #region 计算距离
        public static float count_Distance(ArrayList arr)
        {
            float myDistance = 0;
            for (int i = 0; i < arr.Count - 1; i++)
            {
                try
                {
                    myDistance = myDistance + Vector3.Distance((Vector3) arr[i], (Vector3) arr[i + 1]);
                }
                catch
                {
                    //
                }
            }
            return myDistance;
        }

        public static float count_Distance(List<Vector3> arr)
        {
            float myDistance = 0;
            for (int i = 0; i < arr.Count - 1; i++)
            {
                try
                {
                    myDistance = myDistance + Vector3.Distance(arr[i], arr[i + 1]);
                }
                catch
                {
                    //
                }
            }
            return myDistance;
        }


        public static float count_Distance(Vector3[] arr)
        {
            float myDistance = 0;
            for (int i = 0; i < arr.Length - 1; i++)
            {
                try
                {
                    myDistance = myDistance + Vector3.Distance(arr[i], arr[i + 1]);
                }
                catch
                {
                    //
                }
            }
            return myDistance;
        }
        #endregion

        public static void SetAlpha(Image image, float a)
        {
            var color = image.color;
            color.a = a;
            image.color = color;
        }

        public static void SetAlpha(RawImage image, float a)
        {
            var color = image.color;
            color.a = a;
            image.color = color;
        }


    }
}
